﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Public;
using BUL;
namespace Presentation
{
    public partial class frmManageDataStore : Form
    {
        Role role = new Role();
        User u = new User();
        bool hasClick_btnTKDT_TH_N = false;
        bool hasClick_btnTKDT_TH_T = false;
        bool hasClick_btn_TKDT_CT = false;
        bool hasClick_btnTKTC = false;

        public frmManageDataStore()
        {
            InitializeComponent();
            setMonthYear();
            getUser();
            timer1.Start();
        }

        public void Reload_TKDT_TH_N()
        {
            btnTKDT_TH_N.PerformClick();
        }
        public void Reload_TKDT_TH_T()
        {
            btnTKDT_TH_T.PerformClick();
        }

        public void Reload_TKDT_CT()
        {
            btn_TKDT_CT.PerformClick();
        }

        public void Reload_TKTC()
        {
            btnTKTC.PerformClick();
        }

        private void getUser()
        {
            role.RoleId = 2;
            u = new User(role);
            u.UserId = 2;
        }
        
        private void btnTKDT_TH_N_Click(object sender, EventArgs e)
        {
            hasClick_btnTKDT_TH_N = true;
            ManageDataStoreBUL cls = new ManageDataStoreBUL();
            DataTable tb = cls.ThongKeTongHopTheoNgay(dateTimePicker7.Value.ToString("yyyy-MM-dd"), dateTimePicker8.Value.ToString("yyyy-MM-dd"));
            foreach (var series in chart1.Series)
            {
                series.Points.Clear();
            }
            int sum = 0;
            foreach (DataRow row in tb.Rows)
            {
                chart1.Series["Doanh thu"].Points.AddXY(row.ItemArray[0].ToString(), row.ItemArray[1].ToString());
                sum += int.Parse(row.ItemArray[1].ToString());
            }
            dataGridView5.DataSource = tb;
            label8.Text = "Tổng: " + sum;
        }

        private void setMonthYear()
        {
            dateTimePicker9.Format = DateTimePickerFormat.Custom;
            dateTimePicker9.CustomFormat = "yyyy-MM";
            dateTimePicker10.Format = DateTimePickerFormat.Custom;
            dateTimePicker10.CustomFormat = "yyyy-MM";
            dateTimePicker13.Format = DateTimePickerFormat.Custom;
            dateTimePicker13.CustomFormat = "yyyy-MM";
            dateTimePicker14.Format = DateTimePickerFormat.Custom;
            dateTimePicker14.CustomFormat = "yyyy-MM";
        }

        private void btnTKDT_TH_T_Click(object sender, EventArgs e)
        {
            hasClick_btnTKDT_TH_T = true;
            foreach (var series in chart2.Series)
            {
                series.Points.Clear();
            }
            int sum = 0;
            //get the first day of this month
            string f = new DateTime(dateTimePicker9.Value.Year, dateTimePicker9.Value.Month, 1).ToString("yyyy-MM-dd");
            //get the first day of the next month
            string t = new DateTime(dateTimePicker10.Value.AddMonths(1).Year, dateTimePicker10.Value.AddMonths(1).Month, 1).ToString("yyyy-MM-dd");
            ManageDataStoreBUL cls = new ManageDataStoreBUL();
            DataTable tb = cls.ThongKeTongHopTheoThang(f, t);
            foreach (DataRow row in tb.Rows)
            {
                chart2.Series["Doanh thu"].Points.AddXY(row.ItemArray[0].ToString(), row.ItemArray[1].ToString());
                sum += int.Parse(row.ItemArray[1].ToString());
            }
            dataGridView6.DataSource = tb;
            label11.Text = "Tổng: " + sum;
        }

        private void btn_TKDT_CT_Click(object sender, EventArgs e)
        {
            hasClick_btn_TKDT_CT = true;
            ManageDataStoreBUL cls = new ManageDataStoreBUL();
            DataTable tb = cls.ThongKeChiTiet(dateTimePicker12.Value.ToString("yyyy-MM-dd"), dateTimePicker11.Value.ToString("yyyy-MM-dd"));
            dataGridView7.DataSource = tb;
            int sum = 0;
            foreach (var series in chart3.Series)
            {
                series.Points.Clear();
            }
            foreach (DataRow row in tb.Rows)
            {
                sum += int.Parse(row.ItemArray[1].ToString());
                chart3.Series["Số lượng"].Points.AddXY(row.ItemArray[0].ToString(), row.ItemArray[1].ToString());
            }
            label12.Text = "Tổng: " + sum;
        }

        private void btnTKTC_Click(object sender, EventArgs e)
        {
            hasClick_btnTKTC = true;
            //fromMonth
            string f = dateTimePicker14.Value.Month.ToString();
            //toMonth
            string t = dateTimePicker13.Value.Month.ToString();
            ManageDataStoreBUL cls = new ManageDataStoreBUL();
            DataTable tb = cls.ThongKeThuChi(f, t);
            dataGridView8.DataSource = tb;
            foreach (var series in chart4.Series)
            {
                series.Points.Clear();
            }
            foreach (DataRow row in tb.Rows)
            {
                chart4.Series["Chi"].Points.AddXY(row.ItemArray[0].ToString(), row.ItemArray[1].ToString());
                chart4.Series["Thu"].Points.AddXY(row.ItemArray[0].ToString(), row.ItemArray[2].ToString());
            }
        }

        //delegate
        /*private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.reload_TKDT_TH_N = new Form2.Reload_TKDT_TH_N(Reload_TKDT_TH_N);
            f2.reload_TKDT_TH_T = new Form2.Reload_TKDT_TH_T(Reload_TKDT_TH_T);
            f2.reload_TKDT_CT = new Form2.Reload_TKDT_CT(Reload_TKDT_CT);
            f2.reload_TKTC = new Form2.Reload_TKTC(Reload_TKTC);
            f2.Show();
        }*/

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (hasClick_btnTKDT_TH_N)
            {
                btnTKDT_TH_N.PerformClick();
            }
            if (hasClick_btnTKDT_TH_T)
            {
                btnTKDT_TH_T.PerformClick();
            }
            if (hasClick_btn_TKDT_CT)
            {
                btn_TKDT_CT.PerformClick();
            }
            if (hasClick_btnTKTC)
            {
                btnTKTC.PerformClick();
            }
        }

        private void frmManageDataStore_Close(object sender, FormClosingEventArgs e)
        {
            frmMain main=new frmMain();
            this.Hide();
            main.Show();
        }


    }
}
